﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;


namespace AXTFSTool
{
    public partial class AXTFS2Project : Form
    {
        TfsTeamProjectCollection tfsProjects;
        VersionControlServer vcService;

        List<string> changedFiles;

        public AXTFS2Project()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            tfsProjects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(txtServer.Text));
            vcService = (VersionControlServer)tfsProjects.GetService(typeof(VersionControlServer));

            MessageBox.Show("Connected", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.btnConnect.Enabled = false;
        }

        private void btnGetChanges_Click(object sender, EventArgs e)
        {
            changedFiles = new List<string>();

            try
            {
                foreach (Changeset item in vcService.QueryHistory(txtProjectName.Text, VersionSpec.ParseSingleSpec("D" + txtFromDate.Text, null), 0,
                        RecursionType.Full, null, VersionSpec.ParseSingleSpec("D" + txtFromDate.Text, null),
                        null, Int32.MaxValue, true, false))
                {
                    DateTime checkInDate = item.CreationDate;
                    string user = item.Committer;

                    foreach (Change Itemchange in item.Changes)
                    {
                        string filename = Itemchange.Item.ServerItem.Substring(Itemchange.Item.ServerItem.Substring(0, Itemchange.Item.ServerItem.LastIndexOf('/') - 2).LastIndexOf('/') + 1);
                        filename = (string.Format("{0}", filename));

                        if (!this.changedFiles.Contains(filename))
                        {
                            this.changedFiles.Add(filename);
                        }
                    }
                }

                changedFiles.Sort();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            btnGetChanges.Enabled = false;
            MessageBox.Show("Done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TextWriter oFile = new StreamWriter(txtObjectsFile.Text);

            foreach (string s in this.changedFiles)
            {
                oFile.WriteLine(s);
            }

            oFile.Close();

            btnExport.Enabled = false;
            MessageBox.Show("Done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnCreateProject_Click(object sender, EventArgs e)
        {
            TextWriter oFile = new StreamWriter(txtProjectFile.Text);

            string sT = "Exportfile for AOT version 1.0 or later"; oFile.WriteLine(sT);
            sT = "Formatversion: 1"; oFile.WriteLine(sT);
            sT = "";  oFile.WriteLine(sT);
            sT = "***Element: PRN"; oFile.WriteLine(sT);
            sT = ""; oFile.WriteLine(sT);
            sT = "; Microsoft Dynamics AX Project : EVW_Generate unloaded"; oFile.WriteLine(sT);
            sT = "; --------------------------------------------------------------------------------"; oFile.WriteLine(sT);
            sT = "  PROJECTVERSION 2"; oFile.WriteLine(sT);
            sT = ""; oFile.WriteLine(sT);
            sT = "PROJECT #EVW_Generate"; oFile.WriteLine(sT);
            sT = "   SHARED"; oFile.WriteLine(sT);
            sT = "  PROPERTIES"; oFile.WriteLine(sT);
            sT = "    Name                #EVW_Generate"; oFile.WriteLine(sT);
            sT = "  ENDPROPERTIES"; oFile.WriteLine(sT);
            sT = ""; oFile.WriteLine(sT);
            sT = "    PROJECTCLASS ProjectNode"; oFile.WriteLine(sT);
            
            foreach (string s in this.changedFiles)
            {
                string s1;
                s1 = s.Substring(0, s.Length - 4);
                if (s1.ToLower().IndexOf("resources") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "21", "820", "0");
                }
                if (s1.ToLower().IndexOf("output") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "3", "296", "0");
                }
                if (s1.ToLower().IndexOf("action") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "2", "296", "0");
                }
                if (s1.ToLower().IndexOf("display") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "1", "296", "0");
                }
                if (s1.ToLower().IndexOf("menus") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "16", "205", "0");
                }
                if (s1.ToLower().IndexOf("jobs") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "5", "215", "0");
                }
                if (s1.ToLower().IndexOf("queries") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "20", "330", "0");
                }
                if (s1.ToLower().IndexOf("reports") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "18", "202", "0");
                }
                if (s1.ToLower().IndexOf("forms") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "11", "201", "0");
                }
                if (s1.ToLower().IndexOf("classes") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "45", "329", "0");
                }
                if (s1.ToLower().IndexOf("macros") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "4", "218", "0");
                }
                if (s1.ToLower().IndexOf("security keys") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "36", "313", "0");
                }
                if (s1.ToLower().IndexOf("configuration keys") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "35", "312", "0");
                }
                if (s1.ToLower().IndexOf("licence codes") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "15", "311", "0");
                }
                if (s1.ToLower().IndexOf("base enums") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "40", "209", "0");
                }
                if (s1.ToLower().IndexOf("extended data types") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "41", "228", "0");
                }
                if (s1.ToLower().IndexOf("views") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "44", "243", "0");
                }
                if (s1.ToLower().IndexOf("maps") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "44", "236", "0");
                }
                if (s1.ToLower().IndexOf("tables") != -1)
                {
                    this.WriteToPrj(oFile, s1.Substring(s.IndexOf("/") + 1), "44", "204", "0");
                }
            }

            sT = "  ENDPROJECT"; oFile.WriteLine(sT);
            sT = ""; oFile.WriteLine(sT);
            sT = ""; oFile.WriteLine(sT);
            sT = "***Element: END"; oFile.WriteLine(sT);


            oFile.Close();

            btnExport.Enabled = false;
            MessageBox.Show("Done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void WriteToPrj(TextWriter oFile, string sName, string utiltype, string nodetype, string objectid)
        {
            string sT;

            sT = "    BEGINNODE"; oFile.WriteLine(sT);
            sT = "      FILETYPE 0"; oFile.WriteLine(sT);
            sT = "      UTILTYPE " + utiltype; oFile.WriteLine(sT);
            sT = "      UTILOBJECTID " + objectid; oFile.WriteLine(sT);
            sT = "      NODETYPE " + nodetype; oFile.WriteLine(sT);
            sT = "      NAME #" + sName; oFile.WriteLine(sT);
            sT = "    ENDNODE"; oFile.WriteLine(sT);
        }
    }
}
